<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$return=array();
if($_RQ['op']=='edit'){
	$return['fenlei']=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$id = intval($_RQ['id']);
	if ($id) {
		$return['item']=pdo_get('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		//$return['item']['content']=htmlspecialchars_decode($return['item']['content']);
		$return['item']['advs']=json_decode($return['item']['advs'],true);
		//unset($return['item']['status'],$return['item']['adtype'],$return['item']['re_readnum'],$return['item']['re_likenum'],$return['item']['article_rule_daynum'],$return['item']['article_rule_allnum'],$return['item']['article_rule_credit'],$return['item']['article_rule_money'],$return['item']['article_rule_money_total'],$return['item']['article_rule_userd_money'],$return['item']['yingxiao']);
	}else{
		$return['item']=array('advs'=>array());
	}
	if($_SESSION['os']!='windows'){
		$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'desc'=>'文章信息',
			'tabs'=>array(
				array('op'=>'list','title'=>'文章列表'),
				array('op'=>'edit','title'=>'编辑列表'),
			),
			'edit'=>array(
				array('type'=>'string','title'=>'文章标题','name'=>'title', 'help'=>'' ,'must'=>true),
				array('type'=>'string','title'=>'文章排序','name'=>'paixu', 'help'=>'填写100以内正整数，数值越小越靠前' ),
				array('type'=>'string','title'=>'文章费用','name'=>'fee','help'=>'' ),
				array('type'=>'switch','title'=>'是否显示','name'=>'status','help'=>'' ),
				array('type'=>'select','title'=>'文章分类','name'=>'fenlei','options'=>'fenlei' ),
				array('type'=>'richtext','title'=>'文章内容','name'=>'content' , 'help'=>'','must'=>true),
			)
		);
		$return['htmls']=$htmls;
		exi($return);
	}
	$return['step']=$return['sucai']=1;
	$return['date']=date('Y-m-d');
	$return['account_title']=$_SESSION['title'];
	$return['bans']=array(
		'1'=>'禁止复制链接',
		'2'=>'禁止分享至朋友圈',
		'3'=>'禁止分享给好友'
	);
	$return['yingxiaos']=array(
		'0'=>'关闭此功能',
		'1'=>'只显示第一张',
		'2'=>'随机显示',
		'3'=>'轮播显示'
	);
	$return['shows']=array(
		'1'=>'微信端',
		'2'=>'手机端'
	);
	$return['account']=array(
		'1'=>'显示',
		'0'=>'不显示'
	);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$fields=array('title','paixu','fee','fenlei','keyword','readnum','likenum','status','author','adtype','adtitle','admore','date','account','oldurl','article_rule_daynum','article_rule_allnum','article_rule_credit','article_rule_money','article_rule_money_total','article_rule_userd_money','ban','shows','desc','pic_url','content','yingxiao','adurl','advs');
	$data=array_fields($fields,$postdata);
	$data['advs']=ijson_encode($data['advs']);
	if(empty($data['title'])){
		exi('请填写文章标题！','error');
	}
	if(empty($data['fenlei'])){
		exi('请填写文章分类！','error');
	}
	$id=intval($postdata['id']);
	if($id){
		if(!$postdata['createtime']){
			$data['createtime']=TIMESTAMP;
		}
		pdo_update('core_account_article',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_account_article',$data);
		$id=pdo_insertid();
	}
	if($data['keyword']){
		$parameter=array();
		$parameter['action']='article.detail';
		$parameter['id']=$id;
		$parameter['keyword']=$data['keyword'];
		$parameter['title']=$data['title'];
		$parameter['description']=$data['desc'];
		$parameter['picurl']=$data['pic_url'];
		$parameter['status']=$data['status'];
		$parameter['url']=custom_url('core/article.list/detail/id='.$id);
		cfc('core')->save_back_news($parameter);
	}
	exi('编辑文章成功！','','/manage.article_list/');
}

if($_RQ['op']=='get_article'){
	//列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=$params=array();
	$params=array('status'=>1,'uniacid'=>$_SESSION['uniacid']);
	$fields=array('title','id');
	if($_RQ['find']){
		$field=in_array($_RQ['field'],$fields)?$_RQ['field']:'title';
		$params[$field.' like']='%'.$_RQ['find'].'%';
		$return['params']['find']=$_RQ['find'];
		$return['params']['field']=$field;
	}
	$search_fields=array('id','pic_url','createtime','title');
	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$search_fields,'','id desc');
	if($list){
		foreach($list as &$item){
			//整理返回数据
			if($_RQ['many']){
				$item['res']=array('id'=>$item['id'],'title'=>$item['title'],'image'=>$item['pic_url']);
			}else{
				$item['res']=$item['id'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
		    array('name'=>'id','title'=>'文章id','type'=>'string'),
			array('name'=>'pic_url','title'=>'图片','type'=>'image'),
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'res','title_field'=>'id','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'find','select_name'=>'field','select_title'=>'筛选','is_radio'=>true,'options'=>'fields'),
	);
	$return['fields']=array(
		'id'=>'文章ID',
		'title'=>'文章标题',
	);
	exi($return);
}